package com.chy.blog_web.modular.web.controller;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chy.blog_web.core.util.PageAndParamUtil;
import com.chy.blog_web.modular.system.entity.SysMenu;
import com.chy.blog_web.modular.system.entity.SysUser;
import com.chy.blog_web.modular.system.service.ISysMenuService;
import com.chy.blog_web.modular.system.service.ISysUserService;
import com.chy.blog_web.modular.web.entity.BoArticle;
import com.chy.blog_web.modular.web.entity.BoContent;
import com.chy.blog_web.modular.web.entity.BoPhotoAlbum;
import com.chy.blog_web.modular.web.service.IBoArticleService;
import com.chy.blog_web.modular.web.service.IBoContentService;
import com.chy.blog_web.modular.web.service.IBoPhotoAlbumService;
import com.chy.blog_web.modular.web.service.IBoTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：关于我 controller
 * 作者：chenhuiyan
 * 时间：2020/03/06 22:07
 */
@Controller
@RequestMapping(value = "/aboutMe")
public class AboutMeController {

    /**
     * 前缀
     */
    private static String PREFIX = "web/aboutMe/";

    /**
     * 导航service
     */
    @Autowired
    private ISysMenuService sysMenuService;

    /**
     * 内容service
     */
    @Autowired
    private IBoContentService boContentService;

    /**
     * 相册service
     */
    @Autowired
    private IBoPhotoAlbumService boPhotoAlbumService;

    /**
     * 文章标签service
     */
    @Autowired
    private IBoTagService boTagService;

    /**
     * 文章service
     */
    @Autowired
    private IBoArticleService boArticleService;

    /**
     * 管理员service
     */
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 跳转关于我页面
     * @return
     */
    @RequestMapping(value = "")
    public String index(){
        return PREFIX + "aboutMe";
    }

    /**
     * 查询导航、LOGO标题、关于我、页脚、我的相册、文章标签（关联的文章总数）、站长推荐文章、文章点击排行
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAboutMeData")
    public Map<String, Object> getAboutMeData(){
        Map<String, Object> res = new HashMap<>();
        try{
            //查询前台显示导航，并且按创建时间升序排序
            List<SysMenu> sysMenuList = sysMenuService.getBaseMapper()
                    .selectList(new QueryWrapper<SysMenu>()
                            .eq("type", 1)
                            .eq("is_show", 0)
                            .eq("del_flag", 0)
                            .orderByAsc("sort"));

            //查询LOGO标题，编号为：521f1cd04f9d4345af2e62213178d6ac
            BoContent logoTitle = boContentService.getById("521f1cd04f9d4345af2e62213178d6ac");

            //查询关于我，编号为：ca7e5d79092946f98f0e16d28e041135
            BoContent aboutMe = boContentService.getById("ca7e5d79092946f98f0e16d28e041135");

            //查询页脚，编号为：5d8530c950d243e18e57b74190aa2d94
            BoContent indexFooter = boContentService.getById("5d8530c950d243e18e57b74190aa2d94");

            //查询我的相册,设置为显示的、不上锁的、按创建时间降序排列,最多查6条
            List<BoPhotoAlbum> boPhotoAlbumList = boPhotoAlbumService.getBaseMapper()
                    .selectList(new QueryWrapper<BoPhotoAlbum>()
                            .eq("is_show", 0)
                            .eq("is_lock", 0)
                            .eq("del_flag", 0)
                            .orderByDesc("create_date")
                            .last("LIMIT 6"));

            //查询文章标签（关联的文章总数）
            List<Map<String , Object>> boTagList = boTagService.getBoTagList();

            //查询站长推荐、通过审核文章，按创建时间降序,最多查10条
            List<BoArticle> boArticleTopList = boArticleService.getBaseMapper()
                    .selectList(new QueryWrapper<BoArticle>()
                            .eq("is_top", 1)
                            .eq("is_pass", 1)
                            .eq("del_flag", 0)
                            .orderByDesc("create_date")
                            .last("LIMIT 10"));

            //文章点击排行、通过审核文章,按阅读、赞、时间排序。最多查10条
            List<BoArticle> articleClickList = boArticleService.getBaseMapper()
                    .selectList(new QueryWrapper<BoArticle>()
                            .eq("is_pass", 1)
                            .eq("del_flag", 0)
                            .orderByDesc("read_num")
                            .orderByDesc("praise_num")
                            .orderByDesc("create_date")
                            .last("LIMIT 10"));

            res.put("sysMenuList", sysMenuList);
            res.put("logoTitle", logoTitle);
            res.put("aboutMe", aboutMe);
            res.put("indexFooter", indexFooter);
            res.put("boPhotoAlbumList", boPhotoAlbumList);
            res.put("boTagList", boTagList);
            res.put("boArticleTopList", boArticleTopList);
            res.put("articleClickList", articleClickList);
            res.put("state", "200");
            res.put("message", "初始化成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "服务器异常，请重新刷新页面！");
        }

        return res;
    }

    /**
     * 我是博主身份校验(密码校验)
     * @param adminKey
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/validAdmin")
    public Map<String, Object> validAdmin(@RequestParam(name = "adminKey") String adminKey){
        Map<String, Object> res = new HashMap<>();
        try {
            SysUser sysUser = sysUserService.getOne(new QueryWrapper<SysUser>().eq("user_name", "admin").eq("status", 0));
            String adminKeyToMd5 = SecureUtil.md5(adminKey);
            if(adminKeyToMd5.equals(sysUser.getPassword())){
                res.put("state", "200");
                res.put("message", "博主身份验证通过");
            }else{
                res.put("state", "500");
                res.put("message", "再见，您不是博主！");
            }
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "服务器异常，请重新验证！");
        }
        return res;
    }

    /**
     * 我是副博主身份校验(密码校验)
     * @param adminKey
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/validFuAdmin")
    public Map<String, Object> validFuAdmin(@RequestParam(name = "adminKey") String adminKey){
        Map<String, Object> res = new HashMap<>();
        try {
            SysUser sysUser = sysUserService.getOne(new QueryWrapper<SysUser>().eq("user_name", "miao"));
            String adminKeyToMd5 = SecureUtil.md5(adminKey);
            if(adminKeyToMd5.equals(sysUser.getPassword())){
                res.put("state", "200");
                res.put("message", "副博主身份验证通过");
            }else{
                res.put("state", "500");
                res.put("message", "再见，您不是副博主！");
            }
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "服务器异常，请重新验证！");
        }
        return res;
    }

}
